/*
 * Project: BoboDesignPattern
 * 
 * File Created at 2019年5月13日
 * 
 * Copyright 2012 Greenline.com Corporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Greenline Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Greenline.com.
 */
package test.lesson3.tree.binarytree.huffmantree;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @Type HuffmanTree
 * @Desc
 * @author 52489
 * @date 2019年5月13日
 * @Version V1.0
 */
public class HuffmanTree {

    public static void main(String[] args) {
        int[] arr = { 3, 7, 8, 29, 5, 11, 23, 14 };
        creatHuffmanTree(arr);
    }

    // 创建赫夫曼树
    public static Node creatHuffmanTree(int[] arr) {
        List<Node> nodes = new ArrayList<>();
        for (int value : arr) {
            nodes.add(new Node(value));
        }
        Collections.sort(nodes);
        System.out.println(nodes);
        System.out.println(nodes.get(nodes.size() - 1));
        // 循环处理
        // while (nodes.size() > 1) {
        // // 取出权值最小的两个二叉树
        // Collections.sort(nodes);
        // // 最小
        // Node left = nodes.get(nodes.size() - 1);
        // // 次小
        // Node right = nodes.get(nodes.size() - 2);
        // Node parent = new Node(left.value + right.value);
        // // 把取出来的两个二叉树移除
        // nodes.remove(left);
        // nodes.remove(right);
        // // 放入原来的二叉树集合中去
        // nodes.add(parent);
        // }
        return nodes.get(0);
    }

}
